package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        Jedis resource = jedisPool.getResource();
        //获取两个Redis集合的插值
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set != null) {
            for (String picName : set) {
                //删除Redis集合中的文件
                resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES, picName);
                //删除七牛云的文件
                QiniuUtils.deleteFileFromQiniu(picName);
                //System.out.println("自定义任务,清除垃圾图片" + picName);
            }
        }
        //关闭
        resource.close();
    }
}
